﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace TSPquestEDITOR
{
    public partial class basic_info : Form
    {
        public void load_quest_npc_info()
        {
            bgw.ReportProgress(15, "READING START NPC INFO");

            //To Avoid Bugs Lets Blank NPC2/3 ID
            quest_start_npc_2_id_val = 0;
            quest_start_npc_3_id_val = 0;

            SqlConnection qCon = new SqlConnection(connectionString);
            qCon.Open();

            //Select Start NPC_ID 
            SqlCommand quest_start_giver_id = qCon.CreateCommand();
            quest_start_giver_id.CommandText = "SELECT TOP(1) npc_id FROM dbo.QuestLinkResource WHERE quest_id = '" + fin_qID + "' AND flag_start = '1' ORDER BY npc_id ASC";
            int quest_start_giver_id_res = ((int)quest_start_giver_id.ExecuteScalar());
            quest_start_npc_id_val = quest_start_giver_id_res;

            //Select End NPC_ID 
            SqlCommand quest_end_giver_id = qCon.CreateCommand();
            quest_end_giver_id.CommandText = "SELECT TOP(1) npc_id FROM dbo.QuestLinkResource WHERE quest_id = '" + fin_qID + "' AND flag_end = '1'";
            int quest_end_giver_id_res = ((int)quest_end_giver_id.ExecuteScalar());
            quest_end_npc_id_val = quest_end_giver_id_res;

            //Load Start NPC Amount
            SqlCommand quest_start_giver_amount = qCon.CreateCommand();
            quest_start_giver_amount.CommandText = "SELECT count(npc_id) FROM dbo.QuestLinkResource WHERE quest_id = '" + fin_qID + "' AND flag_start = '1'";
            int quest_start_giver_amount_res = ((int)quest_start_giver_amount.ExecuteScalar());
            bgw.ReportProgress(20, "READING START NPC AMOUNT");

            //Set init_npc_id
            int init_npc_id = quest_start_giver_id_res;

            //Set 2nd_init_npc_id
            int init_npc_id_2 = 0;

            //Check the Start NPC Amount
            if (quest_start_giver_amount_res == 1)
            {

                //Select NPC Info Based on NPC_ID
                SqlCommand command = new SqlCommand(
                "SELECT id, text_id, name_text_id, x, y, contact_script FROM dbo.NPCResource WHERE id = '" + quest_start_giver_id_res + "'",
                qCon);

                SqlDataReader npc_1_reader = command.ExecuteReader();

                if (npc_1_reader.HasRows)
                {
                    while (npc_1_reader.Read())
                    {
                        if (npc_1_reader.GetInt32(0) != 0)
                        {
                            quest_start_npc_id_val = npc_1_reader.GetInt32(0);
                            quest_start_npc_title_id_val = npc_1_reader.GetInt32(1);
                            quest_start_npc_name_id_val = npc_1_reader.GetInt32(2);
                            quest_start_npc_x_val = npc_1_reader.GetInt32(3);
                            quest_start_npc_y_val = npc_1_reader.GetInt32(4);
                            quest_start_npc_lua_val = npc_1_reader.GetString(5);
                        }
                        //If it does, we don't want to display him
                        else
                        {
                            quest_start_npc_id_val = 0;
                            quest_start_npc_title_id_val = 0;
                            quest_start_npc_name_id_val = 0;
                            quest_start_npc_x_val = 0;
                            quest_start_npc_y_val = 0;
                            quest_start_npc_lua_val = "";
                        }
                    }
                }
                npc_1_reader.Close();
            }
            if (quest_start_giver_amount_res == 3)
            {

                //We need to load NPC2/3 ID's
                //Select Start NPC_ID 2
                SqlCommand quest_start_giver_2_id = qCon.CreateCommand();
                quest_start_giver_2_id.CommandText = "SELECT TOP(1) npc_id FROM dbo.QuestLinkResource WHERE quest_id = '" + fin_qID + "' AND npc_id != '" + init_npc_id + "' AND flag_start = '1' ORDER BY npc_id ASC";
                int quest_start_giver_2_id_res = ((int)quest_start_giver_2_id.ExecuteScalar());
                quest_start_npc_2_id_val = quest_start_giver_2_id_res;

                //Update init_npc_id_2
                init_npc_id_2 = quest_start_giver_2_id_res;

                //Select Start NPC_ID 3
                SqlCommand quest_start_giver_3_id = qCon.CreateCommand();
                quest_start_giver_3_id.CommandText = "SELECT TOP(1) npc_id FROM dbo.QuestLinkResource WHERE quest_id = '" + fin_qID + "' AND npc_id != '" + init_npc_id + "' AND npc_id != '" + init_npc_id_2 + "' AND flag_start = '1' ORDER BY npc_id ASC";
                int quest_start_giver_3_id_res = ((int)quest_start_giver_3_id.ExecuteScalar());
                quest_start_npc_3_id_val = quest_start_giver_3_id_res;

                //Select NPC Info Based on NPC_ID 1
                SqlCommand npc_1_cmd = new SqlCommand(
                "SELECT id, text_id, name_text_id, x, y, contact_script FROM dbo.NPCResource WHERE id = '" + quest_start_giver_id_res + "'",
                qCon);

                SqlDataReader npc_1_reader = npc_1_cmd.ExecuteReader();

                if (npc_1_reader.HasRows)
                {
                    while (npc_1_reader.Read())
                    {
                        quest_start_npc_id_val = npc_1_reader.GetInt32(0);
                        quest_start_npc_title_id_val = npc_1_reader.GetInt32(1);
                        quest_start_npc_name_id_val = npc_1_reader.GetInt32(2);
                        quest_start_npc_x_val = npc_1_reader.GetInt32(3);
                        quest_start_npc_y_val = npc_1_reader.GetInt32(4);
                        quest_start_npc_lua_val = npc_1_reader.GetString(5);
                    }
                }
                npc_1_reader.Close();

                //Select NPC Info Based on NPC_ID 2

                SqlCommand npc_2_cmd = new SqlCommand(
                "SELECT id, text_id, name_text_id, x, y, contact_script FROM dbo.NPCResource WHERE id = '" + quest_start_giver_2_id_res + "'",
                qCon);

                SqlDataReader npc_2_reader = npc_2_cmd.ExecuteReader();
                if (npc_2_reader.HasRows)
                {
                    while (npc_2_reader.Read())
                    {
                        quest_start_npc_2_id_val = npc_2_reader.GetInt32(0);
                        quest_start_npc_2_title_id_val = npc_2_reader.GetInt32(1);
                        quest_start_npc_2_name_id_val = npc_2_reader.GetInt32(2);
                        quest_start_npc_2_x_val = npc_2_reader.GetInt32(3);
                        quest_start_npc_2_y_val = npc_2_reader.GetInt32(4);
                        quest_start_npc_2_lua_val = npc_2_reader.GetString(5);
                    }
                }
                npc_2_reader.Close();

                //Select NPC Info Based on NPC_ID 3
                SqlCommand npc_3_cmd = new SqlCommand(
                "SELECT id, text_id, name_text_id, x, y, contact_script FROM dbo.NPCResource WHERE id = '" + quest_start_giver_3_id_res + "'",
                qCon);
                SqlDataReader npc_3_reader = npc_3_cmd.ExecuteReader();
                if (npc_3_reader.HasRows)
                {
                    while (npc_3_reader.Read())
                    {
                        quest_start_npc_3_id_val = npc_3_reader.GetInt32(0);
                        quest_start_npc_3_title_id_val = npc_3_reader.GetInt32(1);
                        quest_start_npc_3_name_id_val = npc_3_reader.GetInt32(2);
                        quest_start_npc_3_x_val = npc_3_reader.GetInt32(3);
                        quest_start_npc_3_y_val = npc_3_reader.GetInt32(4);
                        quest_start_npc_3_lua_val = npc_3_reader.GetString(5);

                    }
                }
                npc_3_reader.Close();

            }

            //Select END NPC INFO
            SqlCommand npc_end_cnd = new SqlCommand(
            "SELECT id, text_id, name_text_id, x, y, contact_script FROM dbo.NPCResource WHERE id = '" + quest_end_npc_id_val + "'",
            qCon);
            SqlDataReader npc_end_reader = npc_end_cnd.ExecuteReader();
            if (npc_end_reader.HasRows)
            {
                while (npc_end_reader.Read())
                {
                    if (npc_end_reader.GetInt32(0) != 0)
                    {
                        quest_end_npc_id_val = npc_end_reader.GetInt32(0);
                        quest_end_npc_title_id_val = npc_end_reader.GetInt32(1);
                        quest_end_npc_name_id_val = npc_end_reader.GetInt32(2);
                        quest_end_npc_x_val = npc_end_reader.GetInt32(3);
                        quest_end_npc_y_val = npc_end_reader.GetInt32(4);
                        quest_end_npc_lua_val = npc_end_reader.GetString(5);
                    }
                    else
                    {
                        quest_end_npc_id_val = 0;
                        quest_end_npc_title_id_val = 0;
                        quest_end_npc_name_id_val = 0;
                        quest_end_npc_x_val = 0;
                        quest_end_npc_y_val = 0;
                        quest_end_npc_lua_val = "";
                    }
                }
            }
            npc_end_reader.Close();
            qCon.Close();
        }
    }
}
